All files / src/routes/workout/[id]/_components WorkoutActions.svelte

0% Statements 0/20
0% Branches 0/8
0% Functions 0/8
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
<script lang="ts">
	import type { Enums } from '@strengthsys/shared';
 
	// WorkoutActions — renders Start / Complete / Skip form actions for a workout.
	//
	// Surface-guard alignment per architect-plan §1.2 and spec
	// programming.allium:1092-1107 (WorkoutSession.provides):
	//
	//   status       | Start | Complete | Skip
	//   -------------|-------|----------|-----
	//   scheduled    |   -   |    -     |  -
	//   ready        |   ✓   |    -     |  ✓
	//   in_progress  |   -   |    ✓     |  ✓
	//   completed    |   -   |    -     |  -  (terminal)
	//   skipped      |   -   |    -     |  -  (terminal)
	//
	// Each button is a submit button inside a <form method="POST"> so
	// progressive enhancement works without JS (plain form post).
	// test-id follows the existing `workout-detail-*` convention.
	//
	// Skip is irreversible (spec programming.allium:178 — `skipped` is a
	// terminal state with no un-skip), so it routes through a styled
	// confirmation modal rather than committing on a single tap. The modal is
	// built on the native <dialog> element (free backdrop, focus trap and
	// Esc-to-dismiss) and styled with the shared design tokens, so it matches
	// the rest of the app rather than the browser's system chrome.
	//
	// Progressive enhancement is preserved: the Skip button is a real submit
	// inside <form action="?/skip">. With JS disabled, `requestSkip` never runs
	// and the form posts directly — the workout still skips, honouring the no-JS
	// contract above. With JS, the submit is intercepted to open the modal;
	// confirming calls the form's native submit() (which bypasses the intercept),
	// cancelling just closes the dialog. The modal is a UX safety net on a
	// destructive action, not a correctness gate.
 
	// Single source of truth is the DB `workout_status` enum (via @strengthsys/shared);
	// values propagate through `gen:types`, so this never needs hand-editing on enum change.
	type WorkoutStatus = Enums<'workout_status'>;
 
	interface Props {
		status: WorkoutStatus;
	}
 
	let { status }: Props = $props();
 
	let IIshowStart = $derived(status === 'ready');
	let IshowComplete = $derived(status === 'in_progress');
	let IshowSkip = $derived(status === 'ready' || status === 'in_progress');
 
	let skipForm = $state<HTMLFormElement | null>(null);
	let skipDialog = $state<HTMLDialogElement | null>(null);
 
	// JS on: intercept the Skip submit and confirm via the modal instead of
	// posting immediately. JS off: this handler never runs, so the form posts
	// directly and the skip proceeds — preserving the no-JS contract.
	function requestSkip(event: SubmitEvent) {
		event.preventDefault();
		skipDialog?.showModal();
	}
 
	function confirmSkip() {
		skipDialog?.close();
		// Native submit() bypasses requestSkip (the submit-event handler), so it
		// posts the form for real rather than re-opening the modal.
		skipForm?.submit();
	}
 
	function cancelSkip() {
		skipDialog?.close();
	}
 
	// A click whose target is the <dialog> itself (not the inner panel) is a
	// click on the dimmed backdrop — dismiss, like tapping outside a sheet.
	function onDialogClick(event: MouseEvent) {
		if (event.target === skipDialog) {
			skipDialog?.close();
		}
	}
</script>
 
{#if showStart || showComplete || showSkip}
	<div class="workout-actions">
		{#if showStart}
			<form method="POST" action="?/start">
				<button
					type="submit"
					class="btn btn--primary btn--lg"
					data-testid="workout-action-start"
				>
					Start workout
				</button>
			</form>
		{/if}
 
		{#if showComplete}
			<form method="POST" action="?/complete">
				<button
					type="submit"
					class="btn btn--primary btn--lg"
					data-testid="workout-action-complete"
				>
					Complete workout
				</button>
			</form>
		{/if}
 
		{#if showSkip}
			<form method="POST" action="?/skip" bind:this={skipForm} onsubmit={requestSkip}>
				<button
					type="submit"
					class="btn btn--secondary btn--md"
					data-testid="workout-action-skip"
				>
					Skip workout
				</button>
			</form>
		{/if}
	</div>
{/if}
 
{#if showSkip}
	<dialog
		bind:this={skipDialog}
		class="confirm-modal"
		aria-labelledby="skip-confirm-title"
		aria-describedby="skip-confirm-body"
		data-testid="workout-skip-dialog"
		onclick={onDialogClick}
	>
		<div class="confirm-modal__panel">
			<h2 id="skip-confirm-title" class="confirm-modal__title">Skip this workout?</h2>
			<p id="skip-confirm-body" class="confirm-modal__body">
				Skipping is permanent — this one can't be un-skipped. It won't count
				against you, though; pick up your programme again whenever you're ready.
			</p>
			<div class="confirm-modal__actions">
				<button
					type="button"
					class="btn btn--primary btn--md"
					onclick={cancelSkip}
					data-testid="workout-skip-cancel"
				>
					Keep workout
				</button>
				<button
					type="button"
					class="btn btn--danger btn--md"
					onclick={confirmSkip}
					data-testid="workout-skip-confirm"
				>
					Skip workout
				</button>
			</div>
		</div>
	</dialog>
{/if}
 
<style>
	.workout-actions {
		display: flex;
		flex-wrap: wrap;
		gap: var(--space-3);
		padding-top: var(--space-3);
	}
 
	/* Inline button styles mirrored from $lib/components/Button.svelte.
	   We use plain class names here to avoid adding a component import
	   just for layout — same CSS variables, same tap-target floor.  */
	.btn {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		gap: var(--space-2);
		font-family: var(--font-sans);
		font-weight: 500;
		letter-spacing: -0.005em;
		border-radius: var(--radius-md);
		border: 1px solid transparent;
		cursor: pointer;
		text-align: center;
		min-height: 44px;
	}
 
	.btn:disabled {
		opacity: 0.55;
		cursor: not-allowed;
	}
 
	.btn--md {
		padding: 11px 18px;
		font-size: 14px;
	}
 
	.btn--lg {
		padding: 14px 24px;
		font-size: 16px;
	}
 
	.btn--primary {
		background: var(--primary);
		color: #fff;
		border-color: var(--primary);
	}
 
	.btn--primary:not(:disabled):hover {
		background: var(--primary-hover);
		border-color: var(--primary-hover);
	}
 
	.btn--secondary {
		background: var(--surface);
		color: var(--ink);
		border-color: var(--line);
	}
 
	.btn--secondary:not(:disabled):hover {
		background: var(--surface-alt);
	}
 
	/* Destructive action — a danger-tinted ghost button rather than a loud
	   red fill, matching the app's non-judgmental skip framing while still
	   reading as the irreversible choice. Uses --danger / --coral-soft, which
	   stay legible in both the light and dark themes. */
	.btn--danger {
		background: transparent;
		color: var(--danger);
		border-color: var(--danger);
	}
 
	.btn--danger:not(:disabled):hover {
		background: var(--coral-soft);
		color: var(--on-coral-soft);
		border-color: var(--coral);
	}
 
	/* Confirmation modal — styled native <dialog>. The <dialog> element holds
	   no padding so a click on the dimmed ::backdrop targets it (not the panel),
	   letting onDialogClick distinguish a backdrop tap from a content tap. */
	.confirm-modal {
		margin: auto; /* centre in the viewport when opened with showModal() */
		padding: 0;
		max-width: min(28rem, calc(100vw - var(--space-6)));
		width: 100%;
		background: var(--surface);
		color: var(--ink);
		border: 1px solid var(--line);
		border-radius: var(--radius-lg);
		box-shadow: var(--shadow-card);
		overflow: hidden;
	}
 
	.confirm-modal::backdrop {
		background: rgba(0, 0, 0, 0.45);
	}
 
	.confirm-modal__panel {
		padding: var(--space-6);
	}
 
	.confirm-modal__title {
		margin: 0 0 var(--space-3);
		font-family: var(--font-serif);
		font-size: 20px;
		color: var(--ink);
	}
 
	.confirm-modal__body {
		margin: 0 0 var(--space-6);
		color: var(--ink-soft);
		font-size: 15px;
		line-height: 1.5;
	}
 
	.confirm-modal__actions {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-end;
		gap: var(--space-3);
	}
</style>